package com.xul.yygh.order.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xul.hosp.common.helper.HttpRequestHelper;
import com.xul.yygh.hosp.client.HospitalFeignClient;
import com.xul.yygh.order.mapper.PaymentInfoMapper;
import com.xul.yygh.order.service.OrderInfoService;
import com.xul.yygh.order.service.PaymentService;
import model.com.xul.yygh.enums.OrderStatusEnum;
import model.com.xul.yygh.enums.PaymentStatusEnum;
import model.com.xul.yygh.enums.PaymentTypeEnum;
import model.com.xul.yygh.model.order.OrderInfo;
import model.com.xul.yygh.model.order.PaymentInfo;
import model.com.xul.yygh.vo.order.SignInfoVo;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xul
 * @create 2021-07-20 19:46
 */
@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentService {

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private HospitalFeignClient hospitalFeignClient;

    /**
     * 向支付纪录里面添加信息
     *
     * @param order
     * @param status
     */
    @Override
    public void savePaymentInfo(OrderInfo order, Integer status) {
        // 根据订单id和支付类型 查询支付纪录是否有相同的订单
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", order.getId());
        wrapper.eq("payment_type", status);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            return;
        }
        // 没有相同的订单 就添加相应的属性值
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(order.getId());
        paymentInfo.setPaymentType(status);
        paymentInfo.setOutTradeNo(order.getOutTradeNo());
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
        String subject = new DateTime(order.getReserveDate()).toString("yyyy-MM-dd") + "|" +
                order.getHosname() + "|" + order.getDepname() + "|" + order.getTitle();
        paymentInfo.setSubject(subject);
        paymentInfo.setTotalAmount(order.getAmount());
        baseMapper.insert(paymentInfo);

    }

    /**
     * 支付成功 更新订单状态
     *
     * @param out_trade_no
     * @param resultMap
     */
    @Override
    public void paySuccess(String out_trade_no, Map<String, String> resultMap) {
        // 1.根据订单编号得到支付纪录
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("out_trade_no", out_trade_no);
        // 条件查询 支付类型
        wrapper.eq("payment_type", PaymentTypeEnum.WEIXIN.getStatus());
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);
        // 2.更新支付纪录信息
        paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());
        // 设置回调的时间 即支付时间
        paymentInfo.setCallbackTime(new Date());
        // 设置交易编号
        paymentInfo.setTradeNo(resultMap.get("transaction_id"));
        // 设置回调的内容
        paymentInfo.setCallbackContent(resultMap.toString());
        baseMapper.updateById(paymentInfo);
        // 3.根据订单id得到订单信息 更新订单信息
        OrderInfo orderInfo = orderInfoService.getById(paymentInfo.getOrderId());
        orderInfo.setOrderStatus(OrderStatusEnum.PAID.getStatus());
        orderInfoService.updateById(orderInfo);
        // 4.调用医院接口 更新订单支付信息
        SignInfoVo signInfoVo = hospitalFeignClient.getSignInfoVo(orderInfo.getHoscode());
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("hoscode", orderInfo.getHoscode());
        reqMap.put("hosRecordId", orderInfo.getHosRecordId());
        reqMap.put("timestamp", HttpRequestHelper.getTimestamp());
        String sign = HttpRequestHelper.getSign(reqMap, signInfoVo.getSignKey());
        reqMap.put("sign", sign);
        JSONObject result = HttpRequestHelper.sendRequest(reqMap, signInfoVo.getApiUrl() + "/order/updatePayStatus");


    }

    /**
     * 获取支付纪录
     *
     * @param orderId
     * @param paymentType
     * @return
     */
    @Override
    public PaymentInfo getPaymentInfo(Long orderId, Integer paymentType) {
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId);
        wrapper.eq("payment_type", paymentType);
        return baseMapper.selectOne(wrapper);
    }
}
